<template>
  <div class="flex flex-col h-full">
    <SearchContainer @search="getData" @reset="() => formRef.resetFields()">
      <a-form ref="formRef" class="form-style-flex" :model="formState" :colon="false" :label-col="{ style: { width: '70px' } }">
        <a-form-item name="dateRange" label="开始时间" class="w-[320px] mr-2">
          <a-range-picker v-model:value="formState.dateRange" format="YYYY-MM-DD" value-format="YYYY-MM-DD" />
        </a-form-item>
        <a-form-item name="appLsh" class="w-[300px]">
          <a-input v-model:value="formState.appLsh" placeholder="申请单单号" />
        </a-form-item>
      </a-form>
    </SearchContainer>
    <a-card class="mt-2 flex-1">
      <a-space class="mb-3">
        <a-button type="primary" @click="onClickEdit()">新增</a-button>
      </a-space>
      <BaseTable :loading="loading" column-code="000592" fill :data-source="tableData" bordered :pagination="pagination">
        <template #bodyCell="{ column, index, record }">
          <template v-if="column.dataIndex === 'xh'">
            {{ index + 1 }}
          </template>
          <template v-if="column.dataIndex === 'status'">
            <a-tag v-if="record?.status === 1" color="blue">已审核</a-tag>
            <a-tag v-else color="red">未审核</a-tag>
          </template>
          <template v-if="column.dataIndex === 'action'">
            <a-button type="link" size="small" @click="onClickEdit(record)"> 修改 </a-button>
            <a-button type="link" :disabled="!!record?.status" size="small" @click="onClickStatus(record)"> 审核 </a-button>
            <BaseButton confirm-text="是否删除?" type="text" danger size="small" @click="onClickDelete(record)"> 删除 </BaseButton>
          </template>
        </template>
      </BaseTable>
    </a-card>
    <Edit v-model:open="editOpen" :row-data="rowData" @success="getData" />
  </div>
</template>

<script setup>
import { ref } from 'vue'
import { message } from 'ant-design-vue'
import dayjs from 'dayjs'
import { post } from '@/utils/request'
import SearchContainer from '@/components/SearchContainer'
import BaseTable from '@/components/BaseTable'
import Edit from './Edit.vue'

const rowData = ref({})
const editOpen = ref(false)
const formRef = ref(null)
const formState = ref({
  dateRange: [dayjs().format('YYYY-MM-DD'), dayjs().format('YYYY-MM-DD')]
})

const { loading, tableData, pagination, getData } = useTable({
  url: '/bloodm/bldApplicationRecord/getBldApplicationRecordList',
  immediate: true,
  formatParams: () => {
    return {
      ...formState.value,
      startDate: formState.value.dateRange?.[0], // 排序类型1正序2倒序
      endDate: formState.value.dateRange?.[1] //排序字段 1 2 3
    }
  }
})

// 修改
const onClickEdit = (row = {}) => {
  editOpen.value = true
  rowData.value = { ...row }
}

// 审核
const onClickStatus = async (row) => {
  const { code, msg } = await post('/bloodm/bldApplicationRecord/doUpdateBldApplicationRecordStatus', { appLsh: row?.appLsh, status: row?.status ? 0 : 1 })
  if (code !== 0) return message.error(msg || '审核失败')
  message.success(msg)
  getData()
}

// 删除
const onClickDelete = async (row) => {
  const { code, msg } = await post('/bloodm/bldApplicationRecord/doDelBldApplicationRecord', { appLsh: row?.appLsh })
  if (code !== 0) return message.error(msg || '删除失败')
  message.success('删除成功')
  getData()
}
</script>
